<template>
    <a-modal :title="$t('编辑保护任务')" v-model='isShow' :maskClosable='false' v-dragModal>
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol"
            labelAlign="right">
            <a-spin :spinning="loading">
                <a-form-model-item :label="$t('保护任务名称') + ':'" prop="task_name">
                    <a-input v-model="form.task_name" :placeholder="$t('Enter') + $t('保护任务名称')" />
                </a-form-model-item>
                <a-form-model-item :label="$t('保护类型') + ':'" prop="protect_type">
                    <a-radio-group disabled v-model="form.protect_type">
                        <a-radio v-for="item of typeOptions" :value="item.value" :key="item.value">{{ item.label
                            }}</a-radio>
                    </a-radio-group>
                </a-form-model-item>
                <a-form-model-item :label="$t('快照复制平台') + ':'" prop="copy_platform_name">
                    <a-select disabled v-model="form.copy_platform_name" :placeholder="$t('Enter') + $t('保护策略选择')">
                        <a-select-option v-for="item of platformOptions" :value="item.value" :key="item.value">
                            {{ item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item :label="$t('备份存储池') + ':'" prop="backup_pool">
                    <a-select disabled v-model="form.backup_pool" :placeholder="$t('Choose') + $t('备份存储池')">
                        <a-select-option v-for="item of poolOptions" :value="item.value" :key="item.value">
                            {{ item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item :label="$t('块设备') + ':'" prop="image_name">
                    <a-select disabled v-model="form.image_name" :placeholder="$t('Choose') + $t('块设备')">
                        <a-select-option v-for="item of blockOptions" :value="item.value" :key="item.value">
                            {{ item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
                <a-form-model-item :label="$t('复制策略') + ':'" prop="strategy_name">
                    <a-select disabled v-model="form.strategy_name" :placeholder="$t('Choose') + $t('复制策略')">
                        <a-select-option v-for="item of strategyOptions" :value="item.value" :key="item.value">
                            {{ item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-model-item>
            </a-spin>
        </a-form-model>

        <template slot="footer">
            <a-button type='primary' @click="handleCreate">{{ $t('保存') }}</a-button>
            <a-button @click="handleClose">{{ $t('Cancel') }}</a-button>
        </template>

    </a-modal>
</template>

<script>
export default {
    data() {
        return {
            isShow: false,
            labelCol: { span: 6 },
            wrapperCol: { span: 18 },
            form: {
                task_name: '',
                protect_type: undefined,
                copy_platform_name: undefined,
                backup_pool: undefined,
                image_name: undefined,
                strategy_name: undefined,
            },
            rules: {
                task_name: [
                    { required: true, message: this.$t('Enter') + this.$t('名称'), trigger: 'blur' },
                    { pattern: /^[A-Za-z0-9_]{1,128}$/, message: this.$t('名称仅支持大小写英文字母 + 数字 + 英文下划线，1-128 个字符'), trigger: 'blur' }
                ],
                protect_type: [
                    { required: true, message: this.$t('Choose') + this.$t('保护类型'), trigger: 'change' },
                ],
                copy_platform_name: [
                    { required: true, message: this.$t('Choose') + this.$t('快照复制平台'), trigger: 'change' },
                ],
                backup_pool: [
                    { required: true, message: this.$t('Choose') + this.$t('备份存储池'), trigger: 'change' },
                ],
                image_name: [
                    { required: true, message: this.$t('Choose') + this.$t('块设备'), trigger: 'change' },
                ],
                strategy_name: [
                    { required: true, message: this.$t('Choose') + this.$t('复制策略'), trigger: 'change', validator: (rules, value, callback) => { this.getStrategy(rules, value, callback) } },
                ],
            },
            typeOptions: [
                { label: '快照', value: 2 },
                { label: '日志', value: 1 }
            ],
            platformOptions: [
                { label: '通用 S3 存储平台', value: 1 },
            ],
            poolOptions: [
                { label: '通用 S3 存储平台', value: 1 },
            ],
            blockOptions: [
                { label: '通用 S3 存储平台', value: 1 },
            ],
            strategyOptions: [
                { label: '', value: 1 },
            ],
            loading: false,
            directory: '',
            id: '',
            level: '',
        };
    },
    methods: {
        getStrategy(rule, value, callback) {
            if (!value) callback()
            else callback()
        },
        handleCreate() {
            this.$refs.ruleForm.validate(async valid => {
                if (valid) {
                    // const params = {
                    //     // name: this.form.name,
                    //     // protect_type: this.form.type,
                    //     // copy_platform_id: this.form.platform,
                    //     // image_id: this.form.block,
                    //     // copy_strategy_id: this.form.strategy,
                    // }
                    this.loading = true
                    const { data } = await this.$api.data.modifyTaskCopyApi(this.form.id, { name: this.form.task_name })
                    this.model(data.id) //返回模板
                    this.notice(data.id, data.params, data.flag) //通知列表显示
                    if (data.flag == '0') {
                        this.handleClose()
                        this.$emit('get-list')
                    }
                } else {
                    return false;
                }
                this.loading = false
            });
        },
        handleClose() {
            this.$refs['ruleForm'].resetFields();
            this.isShow = false
        },
    }
}
</script>